<?php
/**
 * Created by PhpStorm.
 * User: Acer
 * Date: 2022/7/9
 * Time: 17:00
 */
namespace app\wx\service;

use app\card\service\CardService;
use app\move\models\IdentityCardModel;
use app\move\service\CardUserService;
use  app\wx\models\UserModel;
class AuthorizeService {

    public function s_getUserInfo($openid){
        $UserModel = new UserModel();
        $res = $UserModel->where(['open_id'=>$openid])->find();
        if($res){
            $res = obj_to_arr($res);
            $idCardInfo= $this->s_identity_card_info($res['id']);
            if($idCardInfo) $res['idCardInfo'] = $idCardInfo;
        }
        return $res;
    }

    /**
     * 存储用户信息
     * @param $data
     * @return int|string
     */
    public function s_install_info($data){
        $UserModel = new UserModel();
        $install =[
            'open_id'       =>  $data['openid'],
            'username'      =>  $data['nickname'],
            'head_img'      =>  $data['headimgurl'],
        ];
        $resId =  $UserModel->insertGetId($install);
        if($resId){
          return $this->s_getUserInfo($data['openid']);
        }
        return false;
    }

    public function s_getWxUserIdInfo($uid){
        $UserModel = new UserModel();
        $res = $UserModel->where(['id'=>$uid])->find();
        if($res) {
            $idCardInfo= $this->s_identity_card_info($uid);
            $idCardInfo = obj_to_arr($idCardInfo);
            $res['idCardInfo'] =[];
            if($idCardInfo){
//                $idCardInfo['is_master'] = 0;  // 默认不是主卡
//                // 判断是否存在主卡
                $CardUserService = new CardUserService();
                $masterCard = $CardUserService->user_card_info($uid,1);
//                if($masterCard) $idCardInfo['is_master'] =1;  // 存在主卡
                $res['idCardInfo'] = $idCardInfo;
            }
        }
        return $res;
    }
    // 根据uid获取用户身份信息
    public function s_identity_card_info($uid){
        $IdentityCardModel = new IdentityCardModel();
        return  $IdentityCardModel->where('uid',$uid)->find();
    }
    // 根据did获取用户身份信息
    public function s_identity_did_info($did){
        $IdentityCardModel = new IdentityCardModel();
        return  $IdentityCardModel->where('did',$did)->find();
    }
    // 根据身份编号获取信息
    public function s_identity_code_info($card_code){
        $IdentityCardModel = new IdentityCardModel();
        return  $IdentityCardModel->where('id_card',$card_code)->find();
    }

}